<script>
	export default {
		//先执行（入口），进入小程序，先执行app.vue中的逻辑代码
		//一般邀请信息，获取系统配置，小程序登录在此执行
		onLaunch: function(option) {
			uni.removeStorageSync("popup")
			console.log('App Launch')
			//this.check_token()
			// #ifdef MP-WEIXIN
			this.xcx_login()
			// #endif
			// #ifdef MP-TOUTIAO
			this.dy_login()
			// #endif
			this.get_sys()
			console.log(option)
			if (option.query.scene) {
				let data = this.url_decode(option.query.scene)
				console.log("data:", data)

				switch (data["type"]) {
					case "get_egg":
						this.get_egg(data["id"])
						break;
					case "share":
						this.share(data["id"])
						break;
					case "turntableLog":
						this.get_egg(data["id"], "turntableLog")
						break;
					case "tx":
						this.get_egg(data["id"], "tx")
						break;
					case "bind_share":
						this.get_bind(data["id"])
						break;
					case "share_coupon":
						this.share_coupon(data["id"])
						break;
					case "goods_code":
						this.get_egg(data["id"], "goods_code")



				}
			}
			if (option.query.type) {
				let that=this
				switch (option.query.type) {
					case "share":
						this.share(option.query.id)
						break;
					case "bind_share":
						this.get_bind(option.query.id)
						break;
					case "share_coupon":
					setTimeout(function(){
						that.share_coupon(option.query.id)
					},3000)
						
						break;

				}
			}

		},
		onShow: function() {
			console.log('App Show')
			//uni.setStorageSync("share", true)
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			share_coupon(id) {
				uni.setStorageSync("invite_uid", id)
				this.$model.get("user/coupon/insert_coupon?invite_uid=" + id).then(res => {
					if (res.code == 200) {
						console.log("邀请成功")
					}
				})
			},
			get_egg(id, type = "egg") {
				uni.navigateTo({
					url: "./pages/logout/logout?id=" + id + "&type=" + type
				})
			},
			share(id) {
				uni.setStorageSync("invite_uid", id)
				this.$model.get("user/common/add_shares?id=" + id).then(res => {

				})
			},
			get_bind(id) {
				uni.setStorageSync("invite_uid", id)
				uni.navigateTo({
					url: 'pages/blind_box/blind_go?share_id=' + id
				})
			},
			url_decode(url) {
				let query = decodeURIComponent(url)
				let arr1 = query.split("&")
				let obj = {}
				arr1.forEach(function(value) {
					var split_data = value.split("=")
					if (split_data[0] && split_data[1]) {
						obj[split_data[0]] = split_data[1]
					}
				})
				return obj

			},
			get_infor() {
				this.$model.get("user/get_infor").then(res => {
					if (res.code == 200) {
						uni.setStorageSync("infor", res.data)
					}
				})
			},
			//验证token是否有效
			check_token() {
				let that = this
				this.$model.get("auth/check_token").then(res => {
					if (res.code == 200) {
						if (!res.data.data) {

							that.xcx_login()
						} else {
							that.get_infor()
						}
					}
				})
			},
			//小程序登录
			xcx_login() {
				let that = this
				uni.login({
					provider: 'weixin', //kuaishou
					success: function(loginRes) {
						console.log(loginRes);
						let invite_uid = ""
						invite_uid = uni.getStorageSync("invite_uid")
						that.$model.get("auth/xcx_login?code=" + loginRes.code + "&invite_uid=" + invite_uid)
							.then(res => {
								if (res.code == 200) {
									uni.setStorageSync("token", res.data.token)
									that.get_infor()
								}
							})
					}
				});
			},
			dy_login() {
				let that = this
				uni.login({
					provider: 'toutiao', //kuaishou
					success: function(loginRes) {
						console.log(loginRes);
						let invite_uid = ""
						invite_uid = uni.getStorageSync("invite_uid")
						let data = {
							code: loginRes.code,
							anonymous_code:'',
							invite_uid:invite_uid,

						}
						that.$model.post("auth/dy_login", data)
							.then(res => {
								if (res.code == 200) {
									uni.setStorageSync("token", res.data.token)
									that.get_infor()
								}
							})
					}
				});
			},
			get_sys() {
				this.$model.get("user/common/get_sys").then(res => {
					if (res.code == 200) {
						uni.setStorageSync("sys", res.data)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "colorui/icon.css";
</style>
